package lc

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

func myAtoi(str string) int {
	i := 0
	s := str // 不习惯用str做变量名
	can := make([]string, 0)
	for ; len(can) == 0 && i < len(s); i++ {
		data := string(s[i])
		_, e := strconv.Atoi(data)
		if data == " " {
		} else if data == "-" || data == "+" { //判断下一位能不能变
			if i == len(s)-1 {
				return 0
			} else {
				nextD := string(s[i+1]);
				_, ne := strconv.Atoi(nextD);
				if ne == nil {
					can = append(can, data)
				} else {
					return 0
				}
			}
		} else if e == nil { //如果能装换
			can = append(can, data)
		} else { // 不能转换
			return 0;
		}

	}
	if len(can) == 0 {
		return 0
	}
	for ;i<len(s) ;i++{
		data := string(s[i])
		_,err:=strconv.Atoi(data)
		if err == nil{
			can = append(can, data)

		}else{
			break
		}
	}
	tmp:= strings.Join(can,"")
	res,_:=strconv.Atoi(tmp)
	if res < math.MinInt32{res = math.MinInt32}
	if res >math.MaxInt32{res = math.MaxInt32}
	return res



}

func T8()  {
	i:=myAtoi("   -42")
	fmt.Println(i)
}